﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace SAF.Controls
{
    public static class NativeMethods
    {
        public const uint WM_COPYDATA = 74u;
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder title, int size);
        public static string GetWindowText(IntPtr hWnd, int maxLength)
        {
            StringBuilder stringBuilder = new StringBuilder(maxLength + 1);
            if (NativeMethods.GetWindowText(hWnd, stringBuilder, stringBuilder.Capacity) != 0)
            {
                return stringBuilder.ToString();
            }
            return string.Empty;
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint msg, IntPtr wParam, ref CopyDataStruct lParam, uint flags, uint timeout, out IntPtr result);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
    }

    [return: MarshalAs(UnmanagedType.Bool)]
    public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

    public struct CopyDataStruct
    {
        public IntPtr Padding;
        public int Size;
        public IntPtr Buffer;
        public CopyDataStruct(IntPtr padding, int size, IntPtr buffer)
        {
            this.Padding = padding;
            this.Size = size;
            this.Buffer = buffer;
        }
    }
}
